package com.zhongkai.controller;


import com.alibaba.druid.util.StringUtils;
import com.zhongkai.CookieUtils;
import com.zhongkai.app.service.PowerSyetemService;
import com.zhongkai.app.service.WebRegLogService;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Chinaset;
import com.zhongkai.model3.MenusRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/**
 * web
 * 注册 登录
 */
@Controller
public class WebRegLogController {
    @Autowired
    private WebRegLogService webRegLogService;

    @Autowired
    private PowerSyetemService powerSyetemService;

    /**
     * web
     * 注册
     */
    @RequestMapping(value = "/web/registered", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult registered(
            String user_name, //手机号码
            String pass_word,//密码
            String set_name, //姓名
            String set_sex,         //用户性别
            String set_phone,         //手机号
            @RequestParam(defaultValue = "0") Integer set_role         //角色
/*            , String uProvince //省
            , String uCity //市
            , String uCountry //区
            , String uTown //乡镇*/

    ) {


/*        System.out.println(
                 phone+ //手机号码
                 password+//密码
                 name +//姓名
                 address+         //详细地址
                 uProvince+ //省
                 uCity+ //市
                 uCountry+ //区
                 uTown //乡镇
        );*/

        ZhongkaiResult zhongkaiResult = webRegLogService.user_registered(
                user_name
                , pass_word
                , set_name
                , set_sex
                , set_phone
                , set_role

        );

        return zhongkaiResult;
    }


    /**
     * web登录
     */
    @RequestMapping(value = "/web/login", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public ZhongkaiResult user_login(String username, String password, HttpServletRequest request, HttpServletResponse response) {

        ZhongkaiResult zhongkaiResult = webRegLogService.user_login(username, password);
        // session.setAttribute("user", zhongkaiResult.getData());
        ZhongkaiResult zhongkaiResult2 = null;
        Chinaset chinaset = null;
        if (zhongkaiResult.getStatus() == 200) {

            chinaset = (Chinaset) zhongkaiResult.getData();

            zhongkaiResult2 = powerSyetemService.queryRole(String.valueOf(chinaset.getSetRole())); //查询权限
            if (zhongkaiResult2.getStatus() == 200) { //查询权限成功

                ArrayList<MenusRole> list = (ArrayList<MenusRole>) zhongkaiResult2.getData();

                chinaset.setMenusRoles(list);
            }else { //权限查询失败
                return zhongkaiResult2;
            }

            //设置5天
            CookieUtils.setCookie(request, response, "token", chinaset.getToken(),432000,true);  //设置cookie
            return ZhongkaiResult.ok(chinaset);
        }else {  //用户登录失败
            return zhongkaiResult;
        }



    }




    /**
     * 用户退出 清除token
     *
     */
    @RequestMapping(value = "/web/user_out", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult user_out(HttpServletRequest httpServletRequest) {

        String token = CookieUtils.getCookieValue(httpServletRequest, "token",true);
        if (StringUtils.isEmpty(token)) {
            token = httpServletRequest.getParameter("token");
        }

        ZhongkaiResult zhongkaiResult = webRegLogService.user_out(token);


        return zhongkaiResult;
    }

}
